//
//  LHRecommendTourNotesTableViewCell.m
//  Knight_Tour
//
//  Created by 庞魁 on 15-5-20.
//  Copyright (c) 2015年 TouchOne. All rights reserved.
//

#import "LHRecommendTourNotesTableViewCell.h"
#import "UIImageView+WebCache.h"

@interface LHRecommendTourNotesTableViewCell ()

@property (nonatomic, retain) UIImageView *tourNotesPic;
@property (nonatomic, retain) UIImageView *tourNotesUserPic;
@property (nonatomic, retain) UILabel *themeLabel;
@property (nonatomic, retain) UILabel *durationLabel;
@property (nonatomic, retain) UILabel *browseLabel;
@property (nonatomic, retain) UILabel *countryLabel;
@property (nonatomic, retain) UILabel *userLabel;

@end

@implementation LHRecommendTourNotesTableViewCell

- (void)dealloc
{
    [_tourNotesPic release];
    [_tourNotesUserPic release];
    [_themeLabel release];
    [_durationLabel release];
    [_browseLabel release];
    [_countryLabel release];
    [_userLabel release];
    [super dealloc];
}

/**
 *  自定义初始化
 */
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        // 游记图片
        self.tourNotesPic = [[UIImageView alloc] init];
        
        _tourNotesPic.backgroundColor = [UIColor colorWithRed:0.116 green:0.700 blue:0.669 alpha:1.000];
        
        _tourNotesPic.layer.cornerRadius = 5;
        _tourNotesPic.clipsToBounds = YES;
        
        [self.contentView addSubview:_tourNotesPic];
        
        [_tourNotesPic release];
        
        // 主题
        self.themeLabel = [[UILabel alloc] init];
        
        _themeLabel.numberOfLines = 0;
        
        [_themeLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
        
        [_themeLabel setTextColor:[UIColor whiteColor]];
        
        [_tourNotesPic addSubview:_themeLabel];
        
        [_themeLabel release];
        
        
        // 持续时间
        self.durationLabel = [[UILabel alloc] init];
        
        [_durationLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:13]];
        
        [_durationLabel setTextColor:[UIColor whiteColor]];
        
        [_tourNotesPic addSubview:_durationLabel];
        
        [_durationLabel release];
        
        // 浏览量
        self.browseLabel = [[UILabel alloc] init];
        
        [_browseLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:13]];
        
        [_browseLabel setTextColor:[UIColor whiteColor]];
        
        [_tourNotesPic addSubview:_browseLabel];
        
        [_browseLabel release];
        
        // 国家
        self.countryLabel = [[UILabel alloc] init];
        
        [_tourNotesPic addSubview:_countryLabel];
        
        [_countryLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:13]];
        
        [_countryLabel setTextColor:[UIColor whiteColor]];
        
        [_countryLabel release];
        
        
        // 游记主人公头像
        self.tourNotesUserPic = [[UIImageView alloc] init];
        
        _tourNotesUserPic.backgroundColor = [UIColor colorWithRed:0.426 green:0.195 blue:0.071 alpha:1.000];
        
        _tourNotesUserPic.layer.cornerRadius = 20;
        _tourNotesUserPic.clipsToBounds = YES;
        
        [_tourNotesPic addSubview:_tourNotesUserPic];
        
        [_tourNotesUserPic release];
        
        // 主人公姓名
        self.userLabel = [[UILabel alloc] init];
        
        [_userLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:13]];
        
        [_userLabel setTextColor:[UIColor whiteColor]];
        
        [_tourNotesPic addSubview:_userLabel];
        
        [_userLabel release];
        
    }
    
    return self;
}
/**
 *  设置控件布局信息
 */
- (void)layoutSubviews
{
    [super layoutSubviews];
   
    
    _tourNotesPic.frame = CGRectMake(10, 10, self.frame.size.width - 20, self.frame.size.height - 20);
    
    
    CGFloat f = [[self class] heightWithText:_themeLabel.text];
    _themeLabel.frame = CGRectMake(15, 10, 270, f);
    
    _durationLabel.frame = CGRectMake(15, _themeLabel.frame.origin.y + _themeLabel.frame.size.height + 6, 50, 20);
    
    _browseLabel.frame = CGRectMake(_durationLabel.frame.origin.x + _durationLabel.frame.size.width , _themeLabel.frame.origin.y + _themeLabel.frame.size.height + 6, 100, 20);
    
    _countryLabel.frame = CGRectMake(15, _durationLabel.frame.origin.y + _durationLabel.frame.size.height + 6, 140, 20);
    
    _tourNotesUserPic.frame = CGRectMake(15, _tourNotesPic.frame.size.height - 40, 30, 30);
    
    _userLabel.frame = CGRectMake(_tourNotesUserPic.frame.origin.x + _tourNotesUserPic.frame.size.width + 5, _tourNotesPic.frame.size.height - 35, 180, 20);
    
    
}

/**
 *  重写setter方法
 */

- (void)setTourNotes:(LHRecommendTravelNotesModel *)tourNotes
{
    if (_tourNotes != tourNotes) {
        
        [_tourNotes release];
        
        _tourNotes = [tourNotes retain];
        
    }
    
    [_tourNotesPic sd_setImageWithURL:[NSURL URLWithString:tourNotes.cover_image_1600] placeholderImage:[UIImage imageNamed:@"word4"]];
    
    _themeLabel.text = tourNotes.name;
    
    
    NSString *a = [NSString stringWithFormat:@"%ld天", tourNotes.day_count];
    _durationLabel.text = a;
    
    NSString *b = [NSString stringWithFormat:@"%ld 次浏览", tourNotes.view_count];
    _browseLabel.text = b;
    
    _countryLabel.text = tourNotes.popular_place_str;
    
    [_tourNotesUserPic sd_setImageWithURL:[NSURL URLWithString:[tourNotes.user objectForKey:@"avatar_l"]]];
    
    _userLabel.text = [tourNotes.user objectForKey:@"name"];
    
}

// 文字自适应高度
+ (CGFloat)heightWithText:(NSString *)text
{
    
    // 创建一个字典保存文本属性
    // 保存文本文字大小
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:18], NSFontAttributeName, nil];
    
    // 预设一个尺寸, 文本最大不会超过这个尺寸
    CGSize size = CGSizeMake(270, 1000);
    
    // 根据文本信息获得一个CGRect
    // 参数1:尺寸范围  参数2:文本属性  参数3:按照什么方式获取Rect  参数4:nil
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return rect.size.height;
    
    
}






- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
